package com.centit.jdghgl.controller;

import com.centit.jdghgl.po.Jdghgl;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.centit.jdghgl.service.JdghglManager;
	

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.airplanebaseinfo.service.AirplaneBaseinfoManager;
import com.centit.framework.common.JsonResultUtils;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.security.model.CentitUserDetails;
import com.centit.framework.system.dao.UnitInfoDao;
import com.centit.framework.system.po.UnitInfo;
import com.centit.support.algorithm.UuidOpt;
import com.centit.support.database.utils.PageDesc;
import com.centit.util.LayuiResultUtil;

/**
 * Jdghgl  Controller.
 * create by scaffold 2020-03-10 
 * @author 脚手架，改成开发者的名字
 * T_JDGHGL【机队规划计划管理表】机队规划计划管理表   
*/


@Controller
@RequestMapping("/jdghgl/jdghgl")
public class JdghglController  extends BaseController {
	private static final Log log = LogFactory.getLog(JdghglController.class);
	
	@Resource
	private JdghglManager jdghglMag;
	@Resource
	private UnitInfoDao unitInfoDao;
	@Resource
	private AirplaneBaseinfoManager airplaneBaseinfoMag;

    /**
     * 查询所有   T_JDGHGL【机队规划计划管理表】  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, PageDesc pageDesc, HttpServletRequest request, HttpServletResponse response) {
    	String limit = request.getParameter("limit");
        if (StringUtils.isBlank(limit)) {
            limit = "10";
        }
        pageDesc.setPageSize(Integer.parseInt(limit));
    	Map<String, Object> searchColumn = convertSearchColumn(request);        
        searchColumn.put("dataValid", "T");
        searchColumn.put(CodeBook.SELF_ORDER_BY, " gh_Gxdate desc");
        JSONArray jsonArrayObjects = jdghglMag.listObjectsAsJson( searchColumn, pageDesc);
        for (int i = 0; i < jsonArrayObjects.size(); i++) {
            JSONObject jsonObject = jsonArrayObjects.getJSONObject(i);
            if (StringUtils.isNotEmpty(jsonObject.getString("ghType"))) {
                jsonObject.put("ghType", CodeRepositoryUtil.getValue("GHTYPE", jsonObject.getString("ghType")));
            }
            if (StringUtils.isNotEmpty(jsonObject.getString("ghStatus"))) {
            	if (jsonObject.getString("ghStatus").equals("1")) {
                    jsonObject.put("ghStatus", "已发布");
                } else {
                    jsonObject.put("ghStatus", "未发布");
                }
            }
            //查询计划是否被关联
            Map<String, Object> search = new HashMap<String, Object>();        
            search.put("dataValid", "T");
            search.put("gljh", jsonObject.getString("recId"));
            JSONArray jsonAirObjects =  airplaneBaseinfoMag.listObjectsAsJson( search, null);
            if(jsonAirObjects!=null&&jsonAirObjects.size()>0){
            	jsonObject.put("gl", "1");
            }else{
            	jsonObject.put("gl", "0");
            }
            
        } 
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);

        LayuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);
    }
    
    /**
     * 查询单个  T_JDGHGL【机队规划计划管理表】 
	
	 * @param recId  REC_ID
     *  
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.GET})
    public void getJdghgl(@PathVariable String recId, HttpServletResponse response) {
    	
    	Jdghgl jdghgl =     			
    			jdghglMag.getObjectById( recId);
        
        JsonResultUtils.writeSingleDataJson(jdghgl, response);
    }
    
    /**
	 * 打开编辑单个
	 *
	 * @param recId
	 *            recId
	 *
	 * @param response
	 *            {@link HttpServletResponse}
	 * @return forward
	 */
	@RequestMapping(value = "/{recId}/{type}", method = { RequestMethod.GET })
	public ModelAndView toAzdbqry(@PathVariable String recId,@PathVariable String type,
			HttpServletResponse response, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		CentitUserDetails cud = getLoginUser(request);
		
		String forward = "forward:/modules/jdghgl/jdghgl/jdghgl-info.jsp";
		Jdghgl jdghgl = jdghglMag.getObjectById(recId);
		if(jdghgl==null){
			jdghgl = new Jdghgl();
		}
		if ("view".equals(type)) {
			forward = "forward:/modules/jdghgl/jdghgl/jdghgl-view.jsp";
		}else{
			jdghgl.setGhEdituser(cud.getUserInfo().getUserName());
			UnitInfo unitInfo =unitInfoDao.getObjectById(cud.getCurrentUnitCode());
			jdghgl.setGhUnitcode(unitInfo.getUnitName());
			 Date currentTime = new Date();
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String dateString = formatter.format(currentTime);
			 jdghgl.setGhGxdate(dateString);
		}
		mv.addObject(OBJECT, jdghgl);
		mv.setViewName(forward);
		return mv;
	}
    /**
     * 新增 T_JDGHGL【机队规划计划管理表】，业务只有新增的时候可用此方法，如有修改可用下面 新增或保存合并的方法
     *
     * @param jdghgl  {@link Jdghgl}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public void createJdghgl(@Valid Jdghgl jdghgl, HttpServletResponse response) {
    	 jdghglMag.saveNewObject(jdghgl);
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    }

    /**
     * 删除单个  T_JDGHGL【机队规划计划管理表】 
	
	 * @param recId  REC_ID
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.DELETE})
    public void deleteJdghgl(@PathVariable String recId, HttpServletResponse response, HttpServletRequest request) {
    	CentitUserDetails loginUser = getLoginUser(request);
    	Jdghgl dbJdghgl  =     			
    			jdghglMag.getObjectById( recId);
    	dbJdghgl.setDataValid("F");
    	dbJdghgl.setDelTime(new Date());
    	dbJdghgl.setDelUsercode(loginUser.getUserCode());
    	jdghglMag.mergeObject(dbJdghgl);
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    } 
    
    /**
     * 发布单个  T_JDGHGL【机队规划计划管理表】 
	
	 * @param recId  REC_ID
     */
    @RequestMapping(value = "/fb/{recId}", method = {RequestMethod.GET})
    public void fbJdghgl(@PathVariable String recId, HttpServletResponse response, HttpServletRequest request) {
    	Jdghgl dbJdghgl  =     			
    			jdghglMag.getObjectById( recId);
    	dbJdghgl.setGhStatus("1");
    	jdghglMag.mergeObject(dbJdghgl);
        JsonResultUtils.writeSingleDataJson("操作成功",response);
    } 
    
    /**
     * 新增或保存 T_JDGHGL【机队规划计划管理表】 
    
	 * @param recId  REC_ID
	 * @param jdghgl  {@link Jdghgl}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{recId}", method = {RequestMethod.PUT})
    public void updateJdghgl(@PathVariable String recId, 
    	@Valid Jdghgl jdghgl, HttpServletResponse response, HttpServletRequest request) {
    	CentitUserDetails cud = getLoginUser(request);
    	
    	Jdghgl dbJdghgl  =     			
    			jdghglMag.getObjectById( recId);

        if (null != dbJdghgl) {//如果数据库数据存在，则是更新操作
        	dbJdghgl .copyNotNullProperty(jdghgl);//赋值页面不为空的值，小心页面空的字符串或者改写copyNotNullProperty方法，根据自己业务需要
        	dbJdghgl.setEditUsercode(cud.getUserCode());
        	dbJdghgl.setEditTime(new Date());
        	jdghglMag.mergeObject(dbJdghgl);
        } else {
        	jdghgl.setRecId(UuidOpt.getUuidAsString32());
        	jdghgl.setDataValid("T");
        	jdghgl.setInsTime(new Date());
        	jdghgl.setInsUsercode(cud.getUserCode());
            jdghglMag.saveNewObject(jdghgl);//新增,注意主键赋值 UuidUtil.getTimeBasedUuid()，填写自己的业务时间，业务操作人等信息
        }

        JsonResultUtils.writeSingleDataJson("操作成功",response);
    }
}
